package com.seteam.collabcrossword.messages.client;

import com.seteam.collabcrossword.CollabCrosswordUtils;
import com.seteam.collabcrossword.Coordinates;
import com.seteam.collabcrossword.Globals;
import com.seteam.collabcrossword.exceptions.CoordinatesException;
import com.seteam.collabcrossword.messages.Message;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * This message will be used by the client to release locks that it
 * currently holds.
 */
public class ReleaseLock implements Serializable, Message {
    private static final long serialVersionUID = 1688878006553556798L;
    private List<Coordinates> coordinates;

    /**
     * Constructor
     *
     * @param coordinates the list of coordinates on which the client desires to release locks
     * @throws CoordinatesException if any of the passed coordinates are null
     */
    public ReleaseLock(final List<Coordinates> coordinates) throws CoordinatesException {
        verify(coordinates);
        this.coordinates = new ArrayList<Coordinates>();
        this.coordinates.addAll(coordinates);
    }

    /**
     * Verifies if any of the coordinates in the passed list are null
     *
     * @param theCoordinates the list of coordinates to be checked
     * @throws CoordinatesException if any of the coordinates are null
     */
    private void verify(final List<Coordinates> theCoordinates) throws CoordinatesException {
        for (final Coordinates coordinate : theCoordinates) {
            CollabCrosswordUtils.checkNotNull(coordinate, Globals.RELEASE_LOCK_ERROR +
                    "No coordinate object passed to ReleaseLock can be null");
        }
    }

    /**
     * Getter for coordinates
     *
     * @return a list of coordinates
     */
    public List<Coordinates> getCoordinatesToUnlock() {
        return coordinates;
    }

    /**
     * {@inheritDoc}
     */
    public String getAction() {
        return Globals.RELEASE_LOCK;
    }
}
